//
//  ILRendererConstants.h
//  ffmpegWrapper
//
//  Created by huchundong on 2016/10/18.
//  Copyright © 2016年 xiaomi. All rights reserved.
//

#ifndef ILRendererConstants_h
#define ILRendererConstants_h
#import <OpenGLES/EAGL.h>
#import <OpenGLES/EAGLDrawable.h>
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>
#import <UIKit/UIKit.h>
// uniform index
enum {
    UNIFORM_YTEXTURE,
    UNIFORM_UTEXTURE,
    UNIFORM_VTEXTURE,
    UNIFORM_COLOR_MATRIX,
    UNIFORM_ZTEXTURE,
    UNIFORM_NOFILTER,
    UNIFORM_ISHD,
    UNIFORM_NOTIMESTAMP,
    UNIFORM_XOFFSET,
    UNIFORM_YOFFSET,
    UNINFOM_CORRECT_USED,
    UNINFOM_CORRECT_RADIUS,
    UNINFOM_CORRECT_OSDX,
    UNINFOM_CORRECT_OSDY,
    UNINFOM_CORRECT_ZOOM,
    NUM_UNIFORMS
};
//GLint uniforms[NUM_UNIFORMS];

// attribute index
enum {
    ATTRIB_VERTEX,
    ATTRIB_TEXCOORD,
    ATTRIB_VERTEX2,
    ATTRIB_TEXCOORD2,
    NUM_ATTRIBUTES
};


typedef NS_ENUM(NSInteger, RenderSplitMode) {
    SPLITMODE_NONE = 0,
    SPLITMODE_H = 1,
    SPLITMODE_LEFT_FULL = 2,
    SPLITMODE_RIGHT_FULL = 3
};


// size of matrix (yuv to rgb translate matrix)
#define MATRIXSIZE (4)

// position(x, y, z) and texturecoord(s, t)
static const GLfloat vertices[] = {
    -1.0f, 1.0f, 0.0f,   0.0f, 0.0f,
     1.0f, 1.0f, 0.0f,   1.0f, 0.0f,
     1.0f, -1.0f, 0.0f,  1.0f, 1.0f,
    -1.0f, -1.0f, 0.0f,  0.0f, 1.0f
};




// position(x, y, z) and texturecoord(s, t)
static const GLfloat mj_vertices[] = {
    -1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
    1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
    1.0f, -1.0f, 0.0f, 1.0f, 1.0f,
    -1.0f, -1.0f, 0.0f, 0.0f, 1.0f
};
static const GLfloat mj_pv[] = {
    -1.0f, 1.0f, 0.0f,1.0f, 1.0f, 0.0f,1.0f, -1.0f, 0.0f,-1.0f, -1.0f, 0.0f,
};
static const GLfloat mj_vv2[] = {
    0.0f, 1.0f,1.0f, 1.0f,1.0f, 0.0f,0.0f, 0.0f
};
static const GLfloat mj_vv[] = {
    0.0f, 0.0f,1.0f, 0.0f,1.0f, 1.0f,0.0f, 1.0f
};
static const GLfloat mj_t_vertices[] = {
    -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
    1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
    1.0f, 1.0f, 0.0f, 1.0f,1.0f,
    -1.0f, 1.0f, 0.0f, 0.0f,1.0f,
};

// vertex index
static const GLushort indices[] = {0,1,2,3};

// yuv to rgb translate matrix
static const GLfloat matrixYCbCr2RGB[MATRIXSIZE * MATRIXSIZE] =
{
    1.164f,  1.164f, 1.164f, 0.0f,
    0.0f, -0.392f, 2.017f, 0.0f,
    1.596f, -0.813f,   0.0f, 0.0f,
    0.0f,    0.0f,   0.0f, 0.0f
};

//const GLfloat matrixYCbCr2RGB[MATRIXSIZE * MATRIXSIZE] =
//{
//    1.164f,  1.164f, 1.164f, 0.0f,
//    0.0f, -0.392f, 2.018f, 0.0f,
//    1.597f, -0.813f,   0.0f, 0.0f,
//    0.0f,    0.0f,   0.0f, 0.0f
//};

//static const GLfloat kColorConversion709[] = {
//    1.164,  1.164, 1.164,
//    0.0, -0.213, 2.112,
//    1.793, -0.533,   0.0,
//};

// vertex shader
static const GLchar vShaderStr[] =
"attribute vec4 position;       \n"
"attribute vec2 inTexcoord;     \n"
"varying vec2 outTexcoord;      \n"
"void main()                    \n"
"{                              \n"
"   gl_Position = position;     \n"
"   outTexcoord = inTexcoord;   \n"
"}                              \n";

// fragment shader
static const GLchar fShaderStr[] =
"precision mediump float;                                          \n"

"varying vec2 outTexcoord;                                         \n"
"uniform sampler2D yTexture;                                       \n"
"uniform sampler2D uTexture;                                       \n"
"uniform sampler2D vTexture;                                       \n"
"uniform mat4 colorMatrix;                                         \n"

"void main()                                                       \n"
"{                                                                 \n"
//"  float y = texture2D(yTexture, outTexcoord).r;\n"
//"  float u = texture2D(uTexture, outTexcoord).r - 0.5;\n"
//"  float v = texture2D(vTexture, outTexcoord).r - 0.5;\n"
//"  gl_FragColor = vec4(y + 1.403 * v, "
//"                      y - 0.344 * u - 0.714 * v, "
//"                      y + 1.77 * u, 1.0);\n"
"  vec4 yuv;                                                       \n"
"  yuv.rgba = vec4((texture2D(yTexture, outTexcoord).r - 0.0625),  \n"
"                  (texture2D(uTexture, outTexcoord).r - 0.500),   \n"
"                  (texture2D(vTexture, outTexcoord).r - 0.500),   \n"
"                  0.0);                                           \n"
"  gl_FragColor = colorMatrix * yuv;                               \n"
"}                                                                 \n";

#define MIJIA_STRINGIZE(x) #x
#define MJJIA_STRINGIZE2(x) MIJIA_STRINGIZE(x)
#define MIJIA_SHADER_STRING(text) @ MJJIA_STRINGIZE2(text)

static NSString *const kLensVertexShaderString = MIJIA_SHADER_STRING
(
 attribute vec4 np;
 attribute vec4 position;
 attribute vec4 inputTextureCoordinate;
 varying vec2 textureCoordinate;
 
 void main()
 {
     gl_Position = position;
     textureCoordinate = inputTextureCoordinate.xy;
 }
 );

static NSString* const kLensFrameShader = MIJIA_SHADER_STRING(
precision mediump float;
varying vec2 textureCoordinate;
uniform sampler2D inputImageTexture0;
uniform sampler2D inputImageTexture;
uniform int nofilter;
uniform int notimestamp;
uniform int isHD;
uniform float correctionRadius;
uniform float zoom;
uniform float offset;
uniform float offsetY;
                                                              
void main()
{
    vec2 newCoor = textureCoordinate - vec2(0.5,0.5);
    if(nofilter == 0 ){
        gl_FragColor = texture2D(inputImageTexture, textureCoordinate);
    }else{
        if(
           notimestamp == 1
           && textureCoordinate.x > 0.0
           && textureCoordinate.x < offset
           
           && textureCoordinate.y > offsetY
           && textureCoordinate.y < 1.0
           ){
            gl_FragColor = texture2D(inputImageTexture, textureCoordinate);
        }else{
            float dis = length(newCoor);
            float r = dis / 1.2;
            float theta = 1.0*1.0;
            if(r>0.0){
                theta = 1.0*atan(r) / r;
            }
            vec2 newTextureCoor = vec2(0.5,0.5)+newCoor*theta;
            gl_FragColor = texture2D(inputImageTexture, newTextureCoor);
        }
        
    }
    
//
}
                                                 );
//GLint attributes[NUM_ATTRIBUTES];

#endif /* IL RendererConstants_h */
